<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  users
 */

class m_users_actions_ajaxMoveUserAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$groupId = $this->getRequestParameter("groupId");
		$rows = $this->getFormValues("row");
		$nbRows = count($rows);
		$lang = $this->getContext()->getDefaultLang();
        $errors = array();
		
		if(!is_null($groupId))
		{
			$group = $this->getDocumentProvider()->getByUniqueId($groupId);
		}
		
		if($nbRows == 1)
		{
			$this->addToModel("uniqueRow", $this->getDocumentProvider()->getByUniqueId($rows[0], $lang));
		}

		if($this->getRequestParameter("nyroModal") == 1)
		{
			if(!is_null($group))
			{
				$relationManager = f_relation_Manager::getInstance();
				
				foreach($rows as $contentId)
				{
					$user = $this->getDocumentProvider()->getByUniqueId($contentId, $lang);
					$parentGroup = $relationManager->getUniqueParent($contentId, $lang);
					$relationManager->clearRelation($parentGroup, $user, "user", $lang);
					$group->addUser($user)->save();
				}
			
				$errors["status"] = "success";
			}
		}
		else
		{
			$groupOptionsTab = m_backoffice_services_TreeService::getInstance()->getSelectArray("users", "modules_users/group", $groupId, false, true, 'users');
		}
		
		$this->addToModel("errors", $errors);
		$this->addToModel("group", $group);
		$this->addToModel("groupOptionsTab", $groupOptionsTab);
		$this->addToModel("lang", $lang);
		$this->addToModel("rows", $rows);
		$this->addToModel("nbRows", $nbRows);
		$this->setView('success');
	}	
	
	public function validate()
	{
        return $this->getUserContext()->hasRight(array("backoffice_accessBo","users_Edit"));
	}
}